package com.yhp.servlet;

import com.yhp.bean.Grade;
import com.yhp.bean.Student;
import com.yhp.dao.StudentDao;
import com.yhp.dao.iml.StudentDaoImpl;
import com.yhp.service.GradeService;
import com.yhp.service.StudentService;
import com.yhp.service.iml.GradeServiceImpl;
import com.yhp.service.iml.StudentServiceImpl;
import com.yhp.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/StudentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("delById".equals(method)) {
            delById(req, resp);
        } else if ("findById".equals(method)) {
            findById(req, resp);
        } else if ("updateStudent".equals(method)) {
            updateStudent(req, resp);
        } else if ("add".equals(method)) {
            insertStudent(req, resp);
        } else if ("validateAdd".equals(method)) {
            //validateAdd(req, resp);
        } else {
            getStudent(req, resp);
        }
    }

    //得到学生列表
    protected void getStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        //1.模糊查询的参数
        int Sex = -1;
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        if (sex != null && sex.length() > 0) {
            Sex = Integer.parseInt(sex);
        }
        req.setAttribute("stuname", stuname);
        req.setAttribute("stuno", stuno);
        req.setAttribute("sex", Sex);
        //1.2获取分页查询参数 limit 开始位置 显示条数
        //页码值(当前页码值)
        String pageIndex = req.getParameter("pageIndex");
        //如果页面没有传任何值过来，就默认为第一页
        int index = pageIndex == null ? 1 : Integer.parseInt(pageIndex);
        PageUtil pageUtil = new PageUtil();
        //存储当前页码值
        req.setAttribute("index", index);
        //获得数据
        StudentService studentService = new StudentServiceImpl();
        List<Student> students = studentService.getStudent(stuname, stuno, Sex, index, pageUtil.getPageSize());
        //获取总页数=总条数%pageSize>0?总条数/pageSize+1:总条数/pageSize
        int total = studentService.total(stuname, stuno, Sex);
        pageUtil.setTotal(total);
        pageUtil.setDataList(students);
        pageUtil.setPageIndex(index);
        req.setAttribute("p1", pageUtil);
        req.setAttribute("allStudent", students);
        req.getRequestDispatcher("list.jsp").forward(req, resp);
    }

    //根据主键查询学生信息
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuid = req.getParameter("stuid");
        StudentDao studentDao = new StudentDaoImpl();
        Student student = studentDao.findById(Integer.parseInt(stuid));

        GradeService gradeService = new GradeServiceImpl();
        List<Grade> grades = gradeService.getGrade();

        req.setAttribute("gradeList", grades);
        req.setAttribute("stu", student);
        req.getRequestDispatcher("edit.jsp").forward(req, resp);
    }

    //更新学生信息
    protected void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int x = 0;
        String stuid = req.getParameter("stuid");
        String stuno1 = req.getParameter("stuno");
        String stuname1 = req.getParameter("stuname");
        String gid = req.getParameter("gid");
        String sex1 = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String polities = req.getParameter("politics");
        String idNumber = req.getParameter("idnumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");

        System.out.println(stuname1 + "," + stuno1 + "," + gid);
        Student student = new Student(stuname1, stuno1, sex1 == null ? -1 : Integer.parseInt(sex1), phone,
                email, registered, address, profession, idNumber, polities, introduction, Integer.parseInt(gid));
        student.setStuid(Integer.parseInt(stuid));

        StudentService studentService = new StudentServiceImpl();
        int i = studentService.updateStudent(student);
        resp.setContentType("text/html;charset=utf-8");
        //resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.println("<script>alert('更新成功!');location.href='/Educational/student/StudentServlet'</script>");
        } else {
            writer.println("<script>alert('更新失败');location.href='/Educational/student/StudentServlet?method=findById&stuid=" + stuid + "'</script>");
        }
    }

    //根据主键删除学生在列表中的信息
    protected void delById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuid = req.getParameter("stuid");

        StudentService studentService = new StudentServiceImpl();
        int i = studentService.delStudent(stuid);
        resp.setContentType("text/html;charset=utf-8");
        //resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.println("<script>alert('删除成功!');location.href='/Educational/student/StudentServlet'</script>");
        } else {
            writer.println("<script>alert('删除失败');location.href='/Educational/student/StudentServlet'</script>");
        }
    }

    //新增学生记录
    protected void insertStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //新增学生记录
        String stuno1 = req.getParameter("stuno");
        String stuname1 = req.getParameter("stuname");
        String gid = req.getParameter("gid");
        String sex1 = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String polities = req.getParameter("politics");
        String idNumber = req.getParameter("idnumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");

        System.out.println(stuname1 + "," + stuno1 + "," + gid);

        Student student = new Student(stuname1, stuno1, sex1 == null ? -1 : Integer.parseInt(sex1), phone,
                email, registered, address, profession, idNumber, polities, introduction, Integer.parseInt(gid));

        StudentService studentService = new StudentServiceImpl();
        int i = studentService.insert(student);
        System.out.println(i);
        resp.setContentType("text/html;charset=utf-8");
        //resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.println("<script>alert('新增成功!');location.href='/Educational/student/StudentServlet'</script>");
        } else {
            writer.println("<script>alert('新增失败');location.href='/Educational/student/getGrade'</script>");
        }
    }

    //异步请求验证新增学生信息是否重复
//    protected void validateAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String stuNo = req.getParameter("stuNo");
//        StudentService studentService = new StudentServiceImpl();
//        Student student = studentService.checkByStuNo(stuNo);
//        resp.setContentType("text/html;charset=utf-8");
//        PrintWriter writer = resp.getWriter();
//        if (student != null) {
//            JSONObject jsonObject = JSONObject.fromObject(student);
//            writer.println(jsonObject);
//        }
  // }
}
